package scau.erps.view.dashboard;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import org.jfree.chart.ChartPanel;

import scau.erps.app.Erps;
import scau.erps.entity.Sand;
import scau.erps.service.SandService;
import scau.erps.service.impl.SandServiceImpl;


/**
 * 竞争情报仪表盘
 * @author DCJ
 *
 */
public class CompetitionPanel  extends JPanel implements ActionListener{
	private static final long serialVersionUID = 1L;
	private JComboBox<String> itemCombo;
	private JButton selectBtn,payBtn;
	private JPanel contentPanel,actionPanel,chartPanel;
	private JTextArea infoTextArea;
	public CompetitionPanel(){				
		contentPanel = new JPanel();
  		contentPanel.setLayout(new BorderLayout());
		contentPanel.add(getActionPanel(),BorderLayout.NORTH);
		contentPanel.add(getChartPanel(),BorderLayout.CENTER);
		contentPanel.add(getInfoTextArea(),BorderLayout.SOUTH);
		this.add(contentPanel);
	}
	/**
	 * 切换不同统计指标Panel
	 * @return
	 */
	private JPanel getActionPanel(){
		String[] items = new String[]{"统计指标","市场占有率","生产能力","研发能力","流动资产","负债","累计利润","所有者权益","综合排名"};	
		itemCombo = new JComboBox<String>();
		for (int i = 0; i < items.length; i++) {
			itemCombo.addItem(items[i]);
		}
		selectBtn = new JButton("选择");		
		selectBtn.addActionListener(this);
		payBtn = new JButton("支付费用");
		payBtn.addActionListener(this);
		actionPanel = new JPanel(); 
		actionPanel.add(itemCombo);		
		actionPanel.add(selectBtn);	
		actionPanel.add(payBtn);
		
		selectBtn.setVisible(false);
		itemCombo.setVisible(false);
		
		return actionPanel;		
	}
	
	/**
	 * 图表Panel，默认加载产品市场预测图
	 * @return
	 */
	private JPanel getChartPanel(){		
		chartPanel = new JPanel(); 
		chartPanel.add(new ChartPanel(ShareChart.getChart()));	
		chartPanel.setVisible(false);
		return chartPanel;
	}
	private void refreshChartPanel(ChartPanel chart){
		chartPanel.removeAll();
		chartPanel.add(chart);
		chartPanel.updateUI();
	}
	private JTextArea getInfoTextArea(){
		infoTextArea = new JTextArea();
		infoTextArea.setBorder(BorderFactory.createTitledBorder("说明"));
		infoTextArea.setText("显示所有竞争对手的运营数据，便于制定有效策略，查看一次需支付1M费用");
		return infoTextArea;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==payBtn){
			Sand sand=Erps.getSand();
			int step=Integer.parseInt(sand.getStep());
			if (!(step>=2 && step<=4)){
				JOptionPane.showMessageDialog(this, "只能在年初阶段查看竞争情报","提示",JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			int cash=sand.getCash();
			int other=sand.getOther();
			if (cash==0) 
				JOptionPane.showMessageDialog(this, "能把现金用得如此干干净净，是在下输了呢_(:3」∠)_","提示",JOptionPane.INFORMATION_MESSAGE);
			else{
				cash=cash-1;                                  //对现金操作
				sand.setCash(cash);
				other=other+1;
				sand.setOther(other);
			    SandService sandService=Erps.getIocInstance().get(SandServiceImpl.class);
			    sandService.updateSand(sand);
			    JOptionPane.showMessageDialog(this, "已支付费用","提示",JOptionPane.INFORMATION_MESSAGE);

			    
			    payBtn.setVisible(false);
				selectBtn.setVisible(true);
				itemCombo.setVisible(true);
				chartPanel.setVisible(true);
				
			}
		}
		if(e.getSource()==selectBtn){
			String item = null;
			if (itemCombo.getSelectedIndex() != 0) {
				item = (String) itemCombo.getSelectedItem();
			}
			if("市场占有率".equals(item)){
				this.refreshChartPanel(new ChartPanel(ShareChart.getChart()));			
			}	
			if("流动资产".equals(item)){
				this.refreshChartPanel(new ChartPanel(CashChart.getChart()));
			}
			if("负债".equals(item)){
				this.refreshChartPanel(new ChartPanel(LoanChart.getChart()));
			}		
			if("累计利润".equals(item)){
				this.refreshChartPanel(new ChartPanel(OldProfitChart.getChart()));
			}
			if("所有者权益".equals(item)){
				this.refreshChartPanel(new ChartPanel(EquityChart.getChart()));
			}
			if("综合排名".equals(item)){
				this.refreshChartPanel(new ChartPanel(RankChart.getChart()));
			}
			if("生产能力".equals(item)){
				this.refreshChartPanel(new ChartPanel(ProductionCapacityChart.getChart()));
			}	
			if("研发能力".equals(item)){
				this.refreshChartPanel(new ChartPanel(ResearchAbilityChart.getChart()));
			}	
		}				
	}
}
